package reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reggie.common.BaseContext;
import reggie.common.R;
import reggie.dto.OrdersDto;
import reggie.entity.OrderDetail;
import reggie.entity.Orders;
import reggie.entity.ShoppingCart;
import reggie.service.OrderDetailService;
import reggie.service.OrdersService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartController shoppingCartController;


    /**
     * 订单分页查询
     *
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime
            , @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<Orders> ordersPage = new Page<>();
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(number != null, Orders::getNumber, number);
        queryWrapper.between(beginTime != null && endTime != null, Orders::getOrderTime, beginTime, endTime);
        queryWrapper.orderByAsc(Orders::getOrderTime).orderByDesc(Orders::getId);
        ordersService.page(ordersPage, queryWrapper);
        return R.success(ordersPage);
    }

    /**
     * 结算购物车
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("购买成功");
    }

    /**
     * 用户订单查询
     *
     * @param
     * @param
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page,int pageSize){
        Long currentId = BaseContext.getCurrentId();
        Page<OrdersDto> ordersDtoPage = new Page<>(page, pageSize);
        ordersService.userPage(currentId,ordersDtoPage);
        return R.success(ordersDtoPage);
    }
    //public R<Map<String, Object>> userPage(int page, int pageSize) {
    //    Long userId = BaseContext.getCurrentId();
    //    Page<Orders> ordersPage = new Page<>();
    //    LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
    //    queryWrapper.eq(Orders::getUserId, userId)
    //            .orderByDesc(Orders::getCheckoutTime);
    //    ordersService.page(ordersPage, queryWrapper);
    //    List<OrdersDto> list = new ArrayList<>();
    //    for (Orders record : ordersPage.getRecords()) {
    //        LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //        lambdaQueryWrapper.eq(OrderDetail::getOrderId, record.getId());
    //        OrdersDto ordersDto = new OrdersDto();
    //        ordersDto.setAddress(record.getAddress());
    //        ordersDto.setConsignee(record.getConsignee());
    //        ordersDto.setUserName(record.getUserName());
    //        ordersDto.setPhone(record.getPhone());
    //        ordersDto.setOrderDetails(orderDetailService.list(lambdaQueryWrapper));
    //        ordersDto.setAmount(record.getAmount());
    //        list.add(ordersDto);
    //    }
    //    Map<String, Object> map =new HashMap<>();
    //    map.put("records", list);
    //    map.put("pages", page);
    //    return R.success(map);
    //}

    /**
     * 修改订单状态
     *
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> put(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        return R.success("修改成功");
    }

    /**
     * 再来一单
     *
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders) {
        shoppingCartController.clean();
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId, orders.getId());
        List<OrderDetail> list = orderDetailService.list(queryWrapper);
        list.forEach(item->{
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setId(item.getId());
            shoppingCart.setName(item.getName());
            shoppingCart.setNumber(item.getNumber());
            shoppingCart.setDishId(item.getDishId());
            shoppingCart.setSetmealId(item.getSetmealId());
            shoppingCart.setImage(item.getImage());
            shoppingCart.setAmount(item.getAmount());
            shoppingCartController.add(shoppingCart);
        });
        return R.success("添加成功");

    }
}
